﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using eTMS.Controllers.SinhVienTrungTuyenService;
using eTMS.Controllers;
using eTMS.Commons;
namespace eTMS.Forms.Students
{
    public partial class FrmSinhVienTrungTuyen : eTMS.Controls.BaseForm
    {
        public FrmSinhVienTrungTuyen()
        {
            InitializeComponent();
        }
        #region Variables
        private SinhVienTrungTuyenController controller = new SinhVienTrungTuyenController();
        #endregion
        #region Screen Functions
        //Load All lable text
        private void LoadLanguage()
        {
            this.Text = SinhVienTrungTuyenStringMapping.SinhVienTrungTuyen;
            this.labelFormTitle.Text = Constants.APP_ADMINISTRATION + " " + SinhVienTrungTuyenStringMapping.SinhVienTrungTuyen;
            this.groupBoxList.Text = Constants.APP_LIST;
            this.ColumnSTT.HeaderText = Constants.APP_INDEX;
            ColumnSoBaoDanh.HeaderText = SinhVienTrungTuyenStringMapping.SoBaoDanh;
            ColumnHoTenSinhVien.HeaderText = SinhVienTrungTuyenStringMapping.HoTenSinhVien;
            ColumnDiaChi.HeaderText = SinhVienTrungTuyenStringMapping.DiaChi;
            ColumnSoCMND.HeaderText = SinhVienTrungTuyenStringMapping.SoCMND;
            ColumnDienThoai.HeaderText = SinhVienTrungTuyenStringMapping.DienThoai;
            ColumnGioiTinh.HeaderText = SinhVienTrungTuyenStringMapping.GioiTinh;
            ColumnNgaySinh.HeaderText = SinhVienTrungTuyenStringMapping.NgaySinh;
            ColumnHeDaoTao_ID.HeaderText = SinhVienTrungTuyenStringMapping.HeDaoTao_ID;
            ColumnHeDaoTao_ID.DefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
            ColumnDanToc.HeaderText = SinhVienTrungTuyenStringMapping.DanToc;
            ColumnTonGiao.HeaderText = SinhVienTrungTuyenStringMapping.TonGiao;
            ColumnQuocGia.HeaderText = SinhVienTrungTuyenStringMapping.QuocGia;
            ColumnNamTNPT.HeaderText = SinhVienTrungTuyenStringMapping.NamTNPT;
            ColumnNamTNPT.DefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
            ColumnDoiTuong.HeaderText = SinhVienTrungTuyenStringMapping.DoiTuong;
            ColumnKhuVuc.HeaderText = SinhVienTrungTuyenStringMapping.KhuVuc;
            ColumnQuanHuyen.HeaderText = SinhVienTrungTuyenStringMapping.QuanHuyen;
            ColumnTinhThanh.HeaderText = SinhVienTrungTuyenStringMapping.TinhThanh;
            ColumnTruongDuTuyen.HeaderText = SinhVienTrungTuyenStringMapping.TruongDuTuyen;
            ColumnNganhDuTuyen.HeaderText = SinhVienTrungTuyenStringMapping.NganhDuTuyen;
            ColumnTruongTrungTuyen.HeaderText = SinhVienTrungTuyenStringMapping.TruongTrungTuyen;
            ColumnNganhTrungTuyen.HeaderText = SinhVienTrungTuyenStringMapping.NganhTrungTuyen;
            ColumnLop12.HeaderText = SinhVienTrungTuyenStringMapping.Lop12;
            ColumnLop11.HeaderText = SinhVienTrungTuyenStringMapping.Lop11;
            ColumnLop10.HeaderText = SinhVienTrungTuyenStringMapping.Lop10;
            ColumnTongDiemThi.HeaderText = SinhVienTrungTuyenStringMapping.TongDiemThi;
            ColumnTongDiemThi.DefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
            ColumnTongDiemUuTien.HeaderText = SinhVienTrungTuyenStringMapping.TongDiemUuTien;
            ColumnIsDaNop.HeaderText = SinhVienTrungTuyenStringMapping.IsDaNop;
            //Search
            this.groupTimHiem.Text = Constants.APP_SEARCH;
            this.groupBoxFilter.Text = Constants.APP_SEARCH_ADVANDCE;
            labelSoBaoDanhFilter.Text = SinhVienTrungTuyenStringMapping.SoBaoDanh;
            cbkHoTenSinhVienFilterExact.Text = Constants.APP_SEARCH_EXACT;
            labelHoTenSinhVienFilter.Text = SinhVienTrungTuyenStringMapping.HoTenSinhVien;
            cbkDiaChiFilterExact.Text = Constants.APP_SEARCH_EXACT;
            labelDiaChiFilter.Text = SinhVienTrungTuyenStringMapping.DiaChi;
            cbkSoCMNDFilterExact.Text = Constants.APP_SEARCH_EXACT;
            labelSoCMNDFilter.Text = SinhVienTrungTuyenStringMapping.SoCMND;
            cbkNgaySinhFilterExact.Text = Constants.APP_SEARCH_EXACT;
            labelNgaySinhFilter.Text = SinhVienTrungTuyenStringMapping.NgaySinh;
            cbkDanTocFilterExact.Text = Constants.APP_SEARCH_EXACT;
            labelDanTocFilter.Text = SinhVienTrungTuyenStringMapping.DanToc;
            cbkTonGiaoFilterExact.Text = Constants.APP_SEARCH_EXACT;
            labelTonGiaoFilter.Text = SinhVienTrungTuyenStringMapping.TonGiao;
            cbkQuocGiaFilterExact.Text = Constants.APP_SEARCH_EXACT;
            labelQuocGiaFilter.Text = SinhVienTrungTuyenStringMapping.QuocGia;
            labelNamTNPTFilter.Text = SinhVienTrungTuyenStringMapping.NamTNPT;
            cbkNamTNPTFilter.Text = Constants.APP_SEARCH;
            cbkDoiTuongFilterExact.Text = Constants.APP_SEARCH_EXACT;
            labelDoiTuongFilter.Text = SinhVienTrungTuyenStringMapping.DoiTuong;
            cbkKhuVucFilterExact.Text = Constants.APP_SEARCH_EXACT;
            labelKhuVucFilter.Text = SinhVienTrungTuyenStringMapping.KhuVuc;
            txtIsDaNopFilter.Items.Add(Constants.APP_ALL);
            txtIsDaNopFilter.Items.Add(Constants.APP_TRUE);
            txtIsDaNopFilter.Items.Add(Constants.APP_FALSE);
            //txtIsDaNopFilter.SelectedIndex = 2;
            labelIsDaNopFilter.Text = SinhVienTrungTuyenStringMapping.IsDaNop;
            labelKhoaHoc_IDFilter.Text = SinhVienTrungTuyenStringMapping.KhoaHoc_ID;
            //end
        }
        //Get entity selected from datagrid
        private SinhVienTrungTuyenEntity GetObject()
        {
            SinhVienTrungTuyenEntity o = controller.MapDataRowToObject(((DataRowView)gridData.SelectedRows[0].DataBoundItem).Row);
            return o;
        }
        //Get entites selected from datagrid
        private List<DataRow> GetSelectedDataRows()
        {
            List<DataRow> ls = new List<DataRow>();
            for (int i = 0; i < gridData.SelectedRows.Count; i++)
            {
                ls.Add(GetDataRow(gridData.SelectedRows[i].Index));
            }
            return ls;
        }
        //Get the selected data row in datagrid
        private DataRow GetSelectedDataRow()
        {
            DataRow row = GetDataRow(gridData.SelectedRows[0].Index);
            return row;
        }
        //Get the selected data row in datagrid
        private DataRow GetDataRow(int index)
        {
            DataRow row = ((DataRowView)gridData.Rows[index].DataBoundItem).Row;
            return row;
        }



        #endregion
        #region Business

        //Delete data from database
        private bool DeleteData()
        {
            List<DataRow> ls = GetSelectedDataRows();
            if (ls.Count == 0) return false;
            bool rval = true;
            try
            {
                gridData.DataSource = null;
                foreach (DataRow row in ls)
                {
                    if (controller.DeleteByID((Int32)row["SinhVienTrungTuyen_ID"]))
                    {
                        this.controller.Table.Rows.Remove(row);
                    }
                    else
                    {
                        rval = false;
                    }
                }
                this.controller.ResetTableIndex();
                gridData.DataSource = this.controller.Table;
            }
            catch
            {
                return false;
            }
            return true;
        }
        #endregion
        #region DataBinding
        //Bind data to datagrid
        private void LoadData()
        {
            gridData.AutoGenerateColumns = false;
            gridData.DataSource = controller.Table;
            gridData.AutoResizeColumns();
        }
        #endregion
        #region Windows Control EventHandlers
        private void FrmSinhVienTrungTuyen_Load(object sender, EventArgs e)
        {
            
            LoadLanguage();
            LoadData();
            KhoaHocController.LoadAllKhoaHoc(false);
            txtKhoaHoc_IDFilter.BindData();
            txtKhoaHoc_IDFilter.SelectedIndex = txtKhoaHoc_IDFilter.Items.Count - 1;
            this.groupBoxFilter.ToggleCollapsed();
            ucCommand.SetSkipVisible(false);
            ucCommand.SetSaveVisible(false);
            ucCommand.SetImportVisible(true);
            ucCommand.ResetAllPos();
        }


        private void ucCommand_OnDelete(object sender, EventArgs e)
        {
            if (gridData.SelectedRows.Count == 0)
            {
                ShowInfo(Constants.MSS_CHOOSERECORD_REQUIRE);
                return;
            }
            if (!Confirm(Constants.MSS_CONFIRM_DELETE)) return;
            DeleteData();
            this.gridData.Focus();
        }

        #endregion
        #region Filters
        private string GetFilter()
        {
            string strFilter = "";
            if (txtSoBaoDanhFilter.Text.Trim() != "")
            {
                strFilter += " AND SoBaoDanh='" + txtSoBaoDanhFilter.Text.Trim() + "'";
            }
            if (txtHoTenSinhVienFilter.Text.Trim() != "")
            {
                if (cbkHoTenSinhVienFilterExact.Checked)
                {
                    strFilter += " AND HoTenSinhVien='" + txtHoTenSinhVienFilter.Text.Trim() + "'";
                }
                else
                {
                    strFilter += " AND HoTenSinhVien LIKE '%" + txtHoTenSinhVienFilter.Text.Trim() + "%'";
                }
            }
            if (txtDiaChiFilter.Text.Trim() != "")
            {
                if (cbkDiaChiFilterExact.Checked)
                {
                    strFilter += " AND DiaChi='" + txtDiaChiFilter.Text.Trim() + "'";
                }
                else
                {
                    strFilter += " AND DiaChi LIKE '%" + txtDiaChiFilter.Text.Trim() + "%'";
                }
            }
            if (txtSoCMNDFilter.Text.Trim() != "")
            {
                if (cbkSoCMNDFilterExact.Checked)
                {
                    strFilter += " AND SoCMND='" + txtSoCMNDFilter.Text.Trim() + "'";
                }
                else
                {
                    strFilter += " AND SoCMND LIKE '%" + txtSoCMNDFilter.Text.Trim() + "%'";
                }
            }

            if (txtNgaySinhFilter.Text.Trim() != "")
            {
                if (cbkNgaySinhFilterExact.Checked)
                {
                    strFilter += " AND NgaySinh='" + txtNgaySinhFilter.Text.Trim() + "'";
                }
                else
                {
                    strFilter += " AND NgaySinh LIKE '%" + txtNgaySinhFilter.Text.Trim() + "%'";
                }
            }

            if (txtDanTocFilter.Text.Trim() != "")
            {
                if (cbkDanTocFilterExact.Checked)
                {
                    strFilter += " AND DanToc='" + txtDanTocFilter.Text.Trim() + "'";
                }
                else
                {
                    strFilter += " AND DanToc LIKE '%" + txtDanTocFilter.Text.Trim() + "%'";
                }
            }
            if (txtTonGiaoFilter.Text.Trim() != "")
            {
                if (cbkTonGiaoFilterExact.Checked)
                {
                    strFilter += " AND TonGiao='" + txtTonGiaoFilter.Text.Trim() + "'";
                }
                else
                {
                    strFilter += " AND TonGiao LIKE '%" + txtTonGiaoFilter.Text.Trim() + "%'";
                }
            }
            if (txtQuocGiaFilter.Text.Trim() != "")
            {
                if (cbkQuocGiaFilterExact.Checked)
                {
                    strFilter += " AND QuocGia='" + txtQuocGiaFilter.Text.Trim() + "'";
                }
                else
                {
                    strFilter += " AND QuocGia LIKE '%" + txtQuocGiaFilter.Text.Trim() + "%'";
                }
            }
            if (cbkNamTNPTFilter.Checked)
            {
                strFilter += " AND NamTNPT=" + txtNamTNPTFilter.Value.ToString();
            }
            if (txtDoiTuongFilter.Text.Trim() != "")
            {
                if (cbkDoiTuongFilterExact.Checked)
                {
                    strFilter += " AND DoiTuong='" + txtDoiTuongFilter.Text.Trim() + "'";
                }
                else
                {
                    strFilter += " AND DoiTuong LIKE '%" + txtDoiTuongFilter.Text.Trim() + "%'";
                }
            }
            if (txtKhuVucFilter.Text.Trim() != "")
            {
                if (cbkKhuVucFilterExact.Checked)
                {
                    strFilter += " AND KhuVuc='" + txtKhuVucFilter.Text.Trim() + "'";
                }
                else
                {
                    strFilter += " AND KhuVuc LIKE '%" + txtKhuVucFilter.Text.Trim() + "%'";
                }
            }

            if (txtIsDaNopFilter.SelectedIndex > 0)
            {
                strFilter += " AND IsDaNop=" + (txtIsDaNopFilter.SelectedIndex == 1 ? "1" : "0");
            }
            if (strFilter.Length > 0)
                strFilter = strFilter.Substring(4);
            return strFilter;
        }
        private void FilterData()
        {
            gridData.ClearSelection();
            controller.LoadAllDSDataFilter((int)txtKhoaHoc_IDFilter.SelectedValue, GetFilter());
            gridData.AutoGenerateColumns = false;
            gridData.DataSource = controller.Table;
            if (gridData.Rows.Count > 0)
            {
                gridData.Focus();
                gridData.ClearSelection();
                gridData.Rows[0].Selected = true;
            }
        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
            FilterData();
        }
        #endregion

        private void ucCommand_OnAddNew(object sender, EventArgs e)
        {
           
        }

        private void ucCommand_OnEdit(object sender, EventArgs e)
        {
            
        }

        private void ucCommand_OnImport(object sender, EventArgs e)
        {
            
        }
    }
}
